package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Category;
import com.ethan.cj.ssm.entity.Content;
import com.ethan.cj.ssm.service.CategoryService;
import com.ethan.cj.ssm.service.ContentService;
import com.ethan.cj.ssm.util.JsonUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Log4j
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 跳转至公告管理页面
     *
     * @return
     */
    @RequestMapping("ContentList")
    public String ContentList() {
        return "contentList";
    }

    /**
     * @return 路径
     */
    @RequestMapping("/getContentByTheme")
    @ResponseBody
    public Map<Object, Object> getContentByTheme(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("theme") String paramString) {
        Map map = JsonUtil.parseObject(paramString, Map.class);
        return contentService.getContentByTheme(page, limit, map);
    }

    /**
     * 根据ID查询该条内容并回显
     *
     * @param cid
     * @param model
     * @return
     */
    @RequestMapping("selectContentCid")
    public String selectContentCid(@RequestParam("cid") int cid, Model model) {
        Content content = contentService.selectContentCid(cid);
        System.out.println("-----------------------" + content.toString());
        model.addAttribute("c", content);
        return "updateContent";
    }

    /**
     * 修改的方法
     *
     * @param contentJson
     * @return
     */
    @RequestMapping("updateContent")
    public Map<Object, Object> updateContent(@RequestParam("content") String contentJson) {
        return contentService.updateContent(contentJson);
    }

    /**
     * 删除的方法
     *
     * @param cid
     * @return
     */
    @RequestMapping("/deleteContentCid")
    @ResponseBody
    public Map<Object, Object> deleteContentCid(@RequestParam("cid") int cid) {
        return contentService.deleteContentCid(cid);
    }

    /**
     * 跳转至添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        List<Category> list = categoryService.selectCategoryList();
        System.out.println(list.get(0));
        model.addAttribute("list", list);
        return "addcontent";
    }
//
//    @RequestMapping("addContent")
//    public JSONObject addContent(String content) {
//        return null;
//    }
}
